package com.zjitc.pigspring.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zjitc.pigspring.bean.po.*;
import com.zjitc.pigspring.common.Codes;
import com.zjitc.pigspring.common.RedisSetNull;
import com.zjitc.pigspring.common.Result;
import com.zjitc.pigspring.service.AddressService;
import com.zjitc.pigspring.service.OrderService;
import com.zjitc.pigspring.service.SellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.security.KeyPair;
import java.util.List;
import java.util.jar.JarFile;

@RestController
@CrossOrigin
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @Autowired
    private OrderService orderService;

    @GetMapping("/getAll")
    public Result<List<Address>> getAll(){
        List<Address> addresses = addressService.list();
        if(addresses!=null){
            return Result.success(addresses);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }

    @PostMapping("/addOne")
    public Result addAddress(@RequestBody Address address){
        QueryWrapper<Address> wrapper = new QueryWrapper<Address>()
                .eq("username",address.getUsername())
                .eq("user_id",address.getUserId())
                .eq("phone",address.getPhone())
                .eq("address",address.getAddress());
        Address one = addressService.getOne(wrapper);
        if (one!=null){
            return Result.error(Codes.ADDRESS_EXISTS);
        }
        boolean save = addressService.save(address);
        if(save){
            return Result.success(Codes.ADD_YES);
        }else{
            return Result.error(Codes.ADD_NO);
        }
    }

    @Transactional
    @DeleteMapping("/deleteOne/{id}")
    @RedisSetNull
    public Result deleteAddressById(@PathVariable Integer id){
        boolean flag = addressService.removeById(id);
        QueryWrapper<Order> queryWrapper = new QueryWrapper<Order>()
                .eq("address_id",id);
        boolean flag1 = orderService.deleteByAddressId(id);
        if (flag && flag1){
            return Result.success(Codes.DELETE_YES);
        }else{
            return Result.error(Codes.DELETE_NO);
        }
    }

    @RedisSetNull
    @Transactional
    @PostMapping("/deleteAll")
    public Result deleteAddresses(@RequestBody List<Integer> ids){
        boolean flag1 = addressService.removeBatchByIds(ids);
        boolean flag2 = false;
        for (Integer id : ids) {
            flag2 = orderService.deleteByAddressId(id);
            if (!flag2){
                break;
            }
        }
        if (flag1 && flag2){
            return Result.success(Codes.DELETE_YES);
        }else{
            return Result.error(Codes.DELETE_NO);
        }
    }

    @GetMapping("/selectOne/{id}")
    public Result<Address> selectOneAddress(@PathVariable Integer id){
        Address address = addressService.getById(id);
        if(address==null){
            return Result.error(Codes.GET_INFO_FAILED);
        }else{
            return Result.success(address);
        }
    }

    @PutMapping("/updateOne")
    public Result updateAddress(@RequestBody Address address){
        QueryWrapper<Address> wrapper = new QueryWrapper<Address>()
                .eq("username",address.getUsername())
                .eq("user_id",address.getUserId())
                .eq("phone",address.getPhone())
                .eq("address",address.getAddress());
        Address one = addressService.getOne(wrapper);
        if (one!=null){
            return Result.error(Codes.ADDRESS_EXISTS);
        }
        boolean flag = addressService.updateById(address);
        if (flag){
            return Result.success(Codes.UPDATE_YES);
        }else{
            return Result.error(Codes.UPDATE_NO);
        }
    }

    @PostMapping("/conditionQuery")
    public Result<List<Address>> querySeller(@RequestBody Address address){
        QueryWrapper<Address> wrapper = new QueryWrapper<Address>()
                .like(StringUtils.isNotBlank(address.getAddress()),"address",address.getAddress());
        List<Address> addresses = addressService.list(wrapper);
        if(addresses!=null){
            return Result.success(addresses);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }

    @GetMapping("/getByUser/{id}")
    public Result<List<Address>> getCollectByUser(@PathVariable Integer id){
        QueryWrapper<Address> queryWrapper = new QueryWrapper<Address>()
                .eq("user_id",id);
        List<Address> list = addressService.list(queryWrapper);
        if (list==null){
            return Result.error(Codes.GET_INFO_FAILED);
        }else{
            return Result.success(list);
        }
    }


}
